package cgl.narada.samples.nbNative;

import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.LinkedList;

import cgl.narada.service.client.ClientService;
import cgl.narada.service.client.SessionService;
import cgl.narada.service.timer.ClockI;
import cgl.narada.service.timer.HRClock;
import cgl.narada.util.UUIDRetriever;

public class DDTest {

    private LinkedList<String> linkEntries;
    
    private Hashtable<String, String> hashEntries;
    
    private LinkedHashMap <String, String> linkHashEntries;
    
    private ClockI hrClock;
    
    public DDTest() throws Exception {
        linkEntries = new LinkedList<String>();
        hashEntries = new Hashtable<String, String>();
        linkHashEntries = new LinkedHashMap<String, String>();
        initializeHRClock();
    }
    
    
    private void initializeHRClock() throws Exception {
        int entityId = 1000;
        /** This file is used to specify the location of the native NB libraries
        that are used for computing the high resolution timestamps. */ 
        String configFile ="C:/NB/NaradaBrokering/config/ServiceConfiguration.txt";
        //String configFile ="/home/shrideep/nb/config/ServiceConfiguration.txt"; 
        SessionService.setServiceConfigurationLocation(configFile);
        ClientService clientService = SessionService.getClientService(entityId);
        hrClock = HRClock.getClock();
        
        System.out.println("\n\n\n\n");
      }
    
    public ClockI getHRClock() {
        return hrClock;
    }
    
    public void addEntry(String identifier) {
        linkEntries.add(identifier);
        hashEntries.put(identifier, identifier);
        linkHashEntries.put(identifier, identifier);
    }
    
    public boolean isDuplicateHash(String identifier) {
        if (hashEntries.containsKey(identifier)) {
            return true;
        }
        return false;
    }
    
    
    public boolean isDuplicateLink(String identifier) {
        if (linkEntries.contains(identifier)) {
            return true;
        }
        return false;
    }
    
    
    public boolean isDuplicateHashMap(String identifier) {
        if (linkHashEntries.containsKey(identifier)) {
            return true;
        }
        return false;
    }
    
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DDTest tester = null;
        try {
            tester = new DDTest();
        } catch (Exception e) {
            System.out.println("Problems initializing Clock" + e);
            e.printStackTrace();
            System.exit(0);
        }
        
        ClockI hrClock = tester.getHRClock();
        
        UUIDRetriever retriever = UUIDRetriever.getInstance();
        
        int numOfTimes = 40000;
        
        for (int i=0; i < numOfTimes; i++) {
            String uuid = retriever.getTimeBasedUUIDAsString();
            tester.addEntry(uuid);
        }

        String identifier = retriever.getTimeBasedUUIDAsString();
        
        long start, stop =0;
        String report = null;
        
        start = hrClock.getTimeMicroseconds();
        tester.isDuplicateHash(identifier); 
        stop = hrClock.getTimeMicroseconds();
        
        report = "Hash Matching interval = (" + (stop-start) + ") micro-Secs";
        System.out.println(report);
        
        start = hrClock.getTimeMicroseconds();
        tester.isDuplicateLink(identifier);
        stop = hrClock.getTimeMicroseconds();
        report = "Link Matching interval = (" + (stop-start) + ") micro-Secs";
        System.out.println(report);
        
        
        start = hrClock.getTimeMicroseconds();
        tester.isDuplicateHashMap(identifier);
        stop = hrClock.getTimeMicroseconds();
        report = "HashMap Matching interval = (" + (stop-start) + ") micro-Secs";
        System.out.println(report);
    }

}
